package ThreadPool;

import javafx.scene.input.DataFormat;
import sun.awt.geom.AreaOp;

import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class socketThread extends Thread{
    private static Socket s = null;
    private  int clientCount = 0;
    private BufferedReader in = null;
    public socketThread(Socket s, int clientCount) {
    this.s = s;
    this.clientCount = clientCount;
        System.out.println("新的端口连接了， 当前的端口总数：" + clientCount + "   线程id：" + Thread.currentThread().getId());
    }

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String str = "";
            while((str = in.readLine())!=null){
            //日期
                Date date = new Date();
                SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
                String time =format1.format(date);
                System.out.println(time + s.getRemoteSocketAddress() + ":" + str + ",客户端id：" + clientCount +
                        ",线程id：" + Thread.currentThread().getId() );
            }
            TCPServer_Thread.clientCount --;
            System.out.println(clientCount + "会话结束，线程id：" + Thread.currentThread().getId());
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
